<?php
namespace app\common\service;

use think\Config;
use think\Log;
use web\util\WechatUtil;
use think\Exception;

class NoticeService {

    /**
     * 默认appid
     *
     * @return string
     */
    public static function getAppId() {
        return get_appid();
    }

    private static function _getTplData($appId = 'wx5a896e9724bebeea') {
        return Config::get("tpl_msg.$appId");
    }

    /**
     * 注册成功消息
     *
     * @para array $data = ['openid':'','name':'','result':'','first','remark','url']
     */
    public static function sendRegSuccessMsg($data) {
        $appId = self::getAppId(); // 'wx5a896e9724bebeea';

        $userId = $data['openid'];
        if (empty($userId)) {
            return;
        }
        $templateId = self::_getTplData($appId)['reg_success_tpl'];
        $name = $data['name'];
        $url = $data['url'];
        $first = $data['first'];
        $remark = $data['remark'];

        $data = array(
            "first" => $first,
            "keyword1" => $name,
            "keyword2" => '注册成功',
            "remark" => $remark
        );
        return self::_send($appId, $templateId, $userId, $data, $url);
    }

    /**
     * 注册失败消息
     *
     * @para array $data = ['openid':'','time':'','first','remark','url']
     */
    public static function sendRegFailMsg($data) {
        $appId = self::getAppId(); // 'wx5a896e9724bebeea';

        $userId = $data['openid'];
        if (empty($userId)) {
            return;
        }
        $templateId = self::_getTplData($appId)['reg_fail_tpl'];
        $first = $data['first'];
        $time = $data['time'];
        $remark = $data['remark'];
        $url = $data['url'];

        $data = array(
            "first" => $first,
            "keyword1" => '注册失败',
            "keyword2" => $time,
            "remark" => $remark
        );
        return self::_send($appId, $templateId, $userId, $data, $url);
    }

    /**
     * 充值成功消息
     *
     * @para array $data = ['openid':'','money':'','time':'','account':'','first','remark','url']
     */
    public static function sendRechargeSuccessMsg($data) {
        $appId = self::getAppId(); // 'wx5a896e9724bebeea';

        $userId = $data['openid'];
        if (empty($userId)) {
            return;
        }
        $templateId = self::_getTplData($appId)['recharge_success_tpl'];
        $first = $data['first'];
        $money = $data['money'];
        $time = $data['time'];
        $account = $data['account'];
        $remark = $data['remark'];
        $url = $data['url'];

        $data = array(
            "first" => $first,
            "keyword1" => $money.'元',
            "keyword2" => $time,
            "keyword3" => $account.'元',
            "remark" => $remark
        );
        return self::_send($appId, $templateId, $userId, $data, $url);
    }

    /**
     * 充值失败消息
     *
     * @para array $data = ['openid':'','result':'','time':'','first','remark','url']
     */
    public static function sendRechargeFailMsg($data) {
        $appId = self::getAppId(); // 'wx5a896e9724bebeea';

        $userId = $data['openid'];
        if (empty($userId)) {
            return;
        }
        $templateId = self::_getTplData($appId)['recharge_fail_tpl'];
        $first = $data['first'];
        $result = $data['result'];
        $time = $data['time'];
        $remark = $data['remark'];
        $url = $data['url'];

        $data = array(
            "first" => $first,
            "keyword1" => $result,
            "keyword2" => $time,
            "remark" => $remark
        );
        return self::_send($appId, $templateId, $userId, $data, $url);
    }

    /**
     * 发送奖励消息
     *
     * @para array $data = ['openid':'','first':'','money':'','time':'',,'beizhu':'','remark':'','url':'']
     */
    public static function sendRewardMsg($data) {
        $appId = self::getAppId(); // 'wx5a896e9724bebeea';

        $userId = $data['openid'];
        if (empty($userId)) {
            return;
        }
        $templateId = self::_getTplData($appId)['account_change_tpl'];
        $first = $data['first'];
        $money = $data['money'];
        $time = $data['time'];
        $beizhu = $data['beizhu'];
        $remark = $data['remark'];
        $url = $data['url'];

        $data = array(
            "first" => $first,
            "keyword1" => sprintf("奖励%s元", $money),
            "keyword2" => $time,
            "keyword3" => $beizhu,
            "remark" => $remark
        );
        
        return self::_send($appId, $templateId, $userId, $data, $url);
    }

    /**
     * 发送账号余额变化消息
     * money 不要加元
     * @para array $data = ['openid':'','first':'','money':'','time':'',,'beizhu':'','remark':'','url':'']
     */
    public static function sendAccountChangeMsg($data) {
        $appId = self::getAppId(); // 'wx5a896e9724bebeea';
        
        $userId = $data['openid'];
        if (empty($userId)) {
            return;
        }
        $templateId = self::_getTplData($appId)['account_change_tpl'];
        $first = $data['first'];
        $money = $data['money'];
        $time = $data['time'];
        $beizhu = $data['beizhu'];
        $remark = $data['remark'];
        $url = $data['url'];
        
        $data = array(
            "first" => $first,
            "keyword1" => $money,
            "keyword2" => $time,
            "keyword3" => $beizhu,
            "remark" => $remark
        );
        return self::_send($appId, $templateId, $userId, $data, $url);
    }
    
    /**
     * 发送订单支付消息
     *
     * @para array $data = ['openid':'','first':'','name':'','order_no':'','money':'','remark':'','url':'']
     */
    public static function sendOrderPayMsg($data) {
        $appId = self::getAppId(); // 'wx5a896e9724bebeea';

        $userId = $data['openid'];
        if (empty($userId)) {
            return;
        }
        $templateId = self::_getTplData($appId)['order_pay_tpl'];
        $first = $data['first'];
        $name = $data['name'];
        $order_no = $data['order_no'];
        $money = $data['money'];
        $remark = $data['remark'];
        $url = $data['url'];

        $data = array(
            "first" => $first,
            "keyword1" => $name,
            "keyword2" => $order_no,
            "keyword3" => $money."元",
            "remark" => $remark
        );
        return self::_send($appId, $templateId, $userId, $data, $url);
    }

    /**
     * 发送订单取消消息
     *
     * @para array $data = ['openid':'','first':'','order_no':'','money':'','remark':'','url':'']
     */
    public static function sendOrderCancelMsg($data) {
        $appId = self::getAppId(); // 'wx5a896e9724bebeea';

        $userId = $data['openid'];
        if (empty($userId)) {
            return;
        }
        $templateId = self::_getTplData($appId)['order_cancel_tpl'];
        $first = $data['first'];
        $order_no = $data['order_no'];
        $money = $data['money'];
        $remark = $data['remark'];
        $url = $data['url'];

        $data = array(
            "first" => $first,
            "keyword1" => $order_no,
            "keyword2" => $money."元",
            "remark" => $remark
        );
        return self::_send($appId, $templateId, $userId, $data, $url);
    }

    /**
     * 发送订单发货消息
     *
     * @para array $data = ['openid':'','first':'','order_no':'','time':'','address':'','remark':'','url':'']
     */
    public static function sendOrderDeliveryMsg($data) {
        $appId = self::getAppId(); // 'wx5a896e9724bebeea';

        $userId = $data['openid'];
        if (empty($userId)) {
            return;
        }
        $templateId = self::_getTplData($appId)['order_delivery_tpl'];
        $first = $data['first'];
        $order_no = $data['order_no'];
        $time = $data['time'];
        $address = $data['address'];
        $remark = $data['remark'];
        $url = $data['url'];

        $data = array(
            "first" => $first,
            "keyword1" => $order_no,
            "keyword2" => '已发货...',
            "keyword3" => $time,
            "keyword4" => $address,
            "remark" => $remark
        );
        return self::_send($appId, $templateId, $userId, $data, $url);
    }

    /**
     * 发送订单完成消息
     *
     * @para array $data = ['openid':'','first':'','order_no':'','detail':'','remark':'','url':'']
     */
    public static function sendOrderFinishMsg($data) {
        $appId = self::getAppId(); // 'wx5a896e9724bebeea';

        $userId = $data['openid'];
        if (empty($userId)) {
            return;
        }
        $templateId = self::_getTplData($appId)['order_finish_tpl'];
        $first = $data['first'];
        $order_no = $data['order_no'];
        $detail = $data['detail'];
        $remark = $data['remark'];
        $url = $data['url'];

        $data = array(
            "first" => $first,
            "keyword1" => $order_no,
            "keyword2" => $detail,
            "remark" => $remark
        );
        return self::_send($appId, $templateId, $userId, $data, $url);
    }

    /**
     * 发送代理邀请成功通知
     *
     * @para array $data = ['openid':'','first':'','id':'','time':'','name':'','remark':'','url':'']
     */
    public static function sendAgentJoinMsg($data) {
        $appId = self::getAppId(); // 'wx5a896e9724bebeea';

        $userId = $data['openid'];
        if (empty($userId)) {
            return;
        }
        $templateId = self::_getTplData($appId)['agent_join_tpl'];
        $first = $data['first'];
        $id = $data['id'];
        $time = $data['time'];
        $name = $data['name'];
        $remark = $data['remark'];
        $url = $data['url'];

        $data = array(
            "first" => $first,
            "keyword1" => $id,
            "keyword2" => $time,
            "keyword3" => $name,
            "remark" => $remark
        );
        return self::_send($appId, $templateId, $userId, $data, $url);
    }

    /**
     * 发送等级升级
     *
     * @para array $data = ['openid':'','first':'','time':'','remark':'','url':'']
     */
    public static function sendLevelChangeMsg($data) {
        $appId = self::getAppId(); // 'wx5a896e9724bebeea';

        $userId = $data['openid'];
        if (empty($userId)) {
            return;
        }
        $templateId = self::_getTplData($appId)['level_change_tpl'];
        $first = $data['first'];
        $time = $data['time'];
        $remark = $data['remark'];
        $url = $data['url'];

        $data = array(
            "first" => $first,
            "keyword1" => "品正商",
            "keyword2" => "品正商V",
            "keyword3" => $time,
            "remark" => $remark
        );
        return self::_send($appId, $templateId, $userId, $data, $url);
    }
    
    

    private static function _send($appId, $templateId, $userId, $data, $url) {
        
        Log::write("send notice msg appid:$appId,templateId:$templateId,userId:$userId,data:".print_r($data,true));
       
        if (ENVIRONMENT != 'production') {
            //测试开发只给这些人发信息
            $openids = Config::get('openids');
            if(in_array($appId, $openids)){
                return true;
            }
        }
        try {
            $app = WechatUtil::getWechatApp($appId);
            $notice = $app->notice;
            $result = $notice->uses($templateId)
            ->url($url)
            ->andData($data)
            ->andReceiver($userId)
            ->send();
            Log::write("send smg:" . print_r($result, true));
            return true;
        } catch (\Exception $e) {
            Log::write("send smg error,msg:" . $e->getMessage());
            return false;
        }
    }
}
